#!/usr/bin/python
# -*- coding: UTF-8 -*-/**-
# Copyright (C) 2008-209 Robert Spilleboudt
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


# main window  RCOpen_C
from Tkinter import *
import  tkFileDialog, tkMessageBox, webbrowser, os
import pickle, Calc, Gui, sys
fichier=Calc.fichier() # file with the models , default filename

#if len(sys.argv)>1:
#    fichier.prePath= sys.argv[1]     # replace the defaultpath
    #fichier.open()
    #mdx=Calc.model(fichier) # initialize a model instance
print "Current folder:"+fichier.prePath
root = Tk()
root.title("RCCL V2 simulation ("+fichier.prePath+")")
can = Canvas(root,bg="grey",height=600,width=800)
debug=False
tx=Calc.Tx()
def renew(): # if .md older than .mdx , export md
    fichier.renewSd(can)
      
def testerr(mdx):
    # displays the error messages returns True if errors
    err=""
    can.delete(ALL)
    for error in mdx.err :
        print error.prt()  # console print
        err=err+error.prt()+"\n"
    can.create_text(20, 20, text=err, anchor=NW,width=780) # canvas print
    if err=="": return False
    else: return True  
def callOpen(): #menu Open
    Calc.TRAVEL=True
    Gui.SelectModel(root, fichier, title="...models")
    can.delete(ALL)
    mdx=Calc.model(fichier)
    message=""
    mdx.open() # fill the model instance with the instructions from the file
    if testerr(mdx) !=True:
        mdx.calc(True) # this is the initial validation
    if testerr(mdx) !=True:    
        #mdx.export()
        Gui.Simul(root, mdx, tx, can,0) #show the simulation window True=automatic mode
        #mdx.export()

def callCompare(): #menu Open a model and a former correct version
    Calc.TRAVEL=False
    fichierold=Calc.fichier()
    Gui.SelectModel(root, fichierold, title="old Model ")
    mdxold=Calc.model(fichierold) # this model is the former and CORRECT model
    mdxold.open()
    Gui.SelectModel(root, fichier, title="new Model ")
    can.delete(ALL)
    mdx=Calc.model(fichier)
    mdx.mdxold=mdxold
    message=""
    mdx.open() # fill the model instance with the instructions from the file
    if testerr(mdx) !=True:
        mdx.calc(True) # this is the initial validation
    if testerr(mdx) !=True:   
        Gui.Simul(root, mdx, tx, can,0) #show the simulation window True=automatic mode
def callReOpen():
    can.delete(ALL)
    Calc.TRAVEL=True
    RCCLinit=open(fichier.getfilename("RCCLinit","mdp"), "r")
    fichier.selectedModel=Calc.cleanLine(RCCLinit.readline())[0]
    RCCLinit.close()
    mdx=Calc.model(fichier)
    message=""
    mdx.open() # fill the model instance with the instructions from the file
    if testerr(mdx) !=True:
        mdx.calc(True) # this is the initial validation
    if testerr(mdx) !=True:    
        Gui.Simul(root, mdx, tx, can,0) #show the simulation window True=automatic mode

def prjExit():
    global root
    root.destroy()

def callhelp(): #menu Help
    #tkMessageBox.showinfo("Help"," Dans le folder Help, Ouvrez le fichier HelpBase.html")
    url="file://"+os.getcwd()+"/Help/Fr/index.html"
    print url
    webbrowser.open_new(url)
    root.wait_window()    
def notyet(): # not yet
    tkMessageBox.showwarning("Not available", "This is not yet programmed")
    return

# create the main menu
menu = Menu(root)
root.config(menu=menu)
menuTx = Menu(menu)
menu.add_cascade(label="Transmitter", menu=menuTx)
menuTx.add_command(label="Renew", command=renew)
menuTx.add_command(label="Exit", command=prjExit)
menuModel = Menu(menu)
menu.add_cascade(label="Model", menu = menuModel)
menuModel.add_command(label="ReOpen", command=callReOpen)
menuModel.add_command(label="Open", command=callOpen)
menuModel.add_command(label="Compare", command=callCompare)
#menuModel.add_command(label="Open +Link Avr", command=callAvr)

menuHelp = Menu(menu)
menu.add_cascade(label="Help", menu=menuHelp)
menuHelp.add_command(label="Documentation", command=callhelp)
can.pack()
mainloop()


